home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS04.ADF / C / timedely.c < prev    next >
C/C++ Source or Header  |  1985-10-30  |  5KB  |  145 lines

  1. /* time delay program v1.0 */
  2.  
  3. /****************************************************************
  4. *                                                               *
  5. * Copyright 1985, Commodore Amiga Inc.  All rights reserved.    *
  6. * No part of this program may be reproduced, transmitted,       *
  7. * transcribed, stored in retrieval system, or translated into   *
  8. * any language or computer language, in any form or by any      *
  9. * means, electronic, mechanical, magnetic, optical, chemical,   *
  10. * manual or otherwise, without the prior written permission of  *
  11. * Commodore Amiga Incorporated, 983 University Ave, #D          *
  12. * Los Gatos, CA 95030                                           *
  13. *                                                               *
  14. ****************************************************************/
  15.  
  16. /* SIMPLE TIMER EXAMPLE PROGRAM: 
  17.  *
  18.  * Includes dynamic allocation of data structures needed to communicate
  19.  * with the timer device as well as the actual device IO
  20.  */
  21.  
  22. #include <exec/types.h>
  23. #include <exec/lists.h>
  24. #include <exec/nodes.h>
  25. #include <exec/ports.h>
  26. #include <exec/io.h>
  27. #include <exec/devices.h>
  28. #include <devices/timer.h>
  29.  
  30. #define SECONDS io_Actual
  31. #define MICROSECONDS io_Length
  32. /* redefine fields in IOStdReq so as to match requirements of a timeval */
  33.  
  34. struct Port *timerport;
  35. struct IOStdReq *timermsg;
  36.  
  37. main()
  38. {
  39.         printf("\ntimer test");
  40.         timedelay(2,0);
  41.         printf("\nAfter 2 seconds delay");
  42.         timedelay(4,0);
  43.         printf("\nAfter 4 seconds delay");
  44.         timedelay(0,500000);    /* 500,000 seconds = 1/2 second */
  45.         printf("\nAfter 1/2 second delay");
  46.         printf("\nExiting now");
  47. }
  48.  
  49. /* *********************************************************************** */
  50. /*      Timer function - timedelay(seconds,microseconds)
  51.  
  52.         Your task is put to sleep for the specified time interval.
  53.  
  54.         If seconds > 0, then UNIT_VBLANK is used, delay is in multiples of
  55.         60ths of a second.  If seconds < 0, then UNIT_MICROHZ is used for 
  56.         more precision.    
  57.  
  58.         Returns value of 0 if no errors, nonzero (and no task sleeping)
  59.         if there were errors.   -1000 means error during CreatePort,
  60.                                 -2000 means error during CreateStdIO,
  61.                                 other errors returned by OpenDevice.
  62.  
  63.         Notice that since this is a multi-tasking system, the delays
  64.         shown here must be considered to be only approximate.
  65.  
  66.         Also note that this function is used primarily to show how
  67.         a timer device is accessed, including the creation of the 
  68.         message port and a message structure (IOStdReq).   Note that
  69.         there is a Delay(interval) function already in the DOS.library.
  70.         (See the DOS developer's manual for details).
  71.  
  72.         The exec_support function CreateStdIO (in amiga.lib) is used
  73.         for convenience here, with two of the data structure's fields
  74.         redefined to match the information requirements of the timer
  75.         in a timeval structure.
  76. */
  77. /* *********************************************************************** */
  78.  
  79. extern struct Port *CreatePort();
  80. extern struct IOStdReq *CreateStdIO();
  81.                          
  82. int 
  83. timedelay(seconds,microseconds)
  84. ULONG seconds,microseconds;
  85. {
  86.         SHORT error;
  87.         error = preparetimer();
  88.         if(error != 0) return(error);
  89.         set_timer(seconds,microseconds);
  90.         deletetimer();
  91.         return(0);
  92. }                       /* end of timedelay */
  93.  
  94. int 
  95. preparetimer()
  96. {
  97.         SHORT error;
  98.         SHORT whichunit;
  99.         
  100.         timerport = CreatePort(0,0);
  101.         if (timerport == NULL) 
  102.                 return(-1000);  /* Error during CreatePort */
  103.  
  104.         timermsg = CreateStdIO(timerport);
  105.         if (timermsg == NULL)
  106.                 {
  107.                 DeletePort(timerport);
  108.                 return(-2000);  /* Error during CreateStdIO */
  109.                 }
  110.         
  111.         if(seconds > 0)
  112.                 whichunit = UNIT_VBLANK;
  113.         else
  114.                 whichunit = UNIT_MICROHZ;
  115.  
  116.         error = OpenDevice(TIMERNAME, whichunit, timermsg, 0);
  117.         if (error != 0)
  118.                 {
  119.                 DeleteStdIO(timermsg);
  120.                 DeletePort(timerport);
  121.                 return(error);  /* Error during OpenDevice */
  122.                 }
  123.         return(0);
  124. }
  125.  
  126. int
  127. set_timer(seconds,microseconds)
  128. ULONG seconds,microseconds;
  129. {
  130.         timermsg->io_Command = TR_ADDREQUEST;   /* add a new timer request */
  131.         timermsg->SECONDS = seconds;            /* seconds */
  132.         timermsg->MICROSECONDS = microseconds;  /* microseconds */
  133.         DoIO( timermsg );                       /* post request to the timer */
  134.                                                 /* goes to sleep till done */
  135.         return(0);
  136. }
  137.  
  138. int
  139. deletetimer()
  140. {
  141.         DeleteStdIO(timermsg);
  142.         DeletePort(timerport);
  143.         return(0);              /* No errors if 0 returned */
  144. }
  145.